package com.hsh.tools;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author 黄思海
 * @date 2021/7/8
 */
public abstract class StrUtils {

    public static boolean isEmpty(String str) {
        return str == null || str.trim().isEmpty();
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean equalsIgnoreEmptyNull(String source, String target) {
        if (isEmpty(source) && isEmpty(target)) {
            return true;
        }
        return Objects.equals(source, target);
    }

    /**
     * @param sourceKt
     * @param targetJxt
     * @return
     */
    public static boolean equals4DoubleString(String sourceKt, String targetJxt) {
        if (StrUtils.isEmpty(sourceKt) && StrUtils.isEmpty(targetJxt)) {
            return true;
        }
        return safeCompare(convert(sourceKt), convert(targetJxt));
    }


    private static BigDecimal convert(String numberStr) {
        if (isEmpty(numberStr)) {
            return null;
        }
        try {
            return new BigDecimal(numberStr.trim()).setScale(3, RoundingMode.HALF_UP);
        } catch (Exception e) {
            return null;
        }
    }

    public static boolean safeCompare(BigDecimal source, BigDecimal target) {
        if (source == null && target == null) {
            return true;
        }
        if (source == null || target == null) {
            return false;
        }
        return source.compareTo(target) == 0;
    }

    public static boolean equalsNumberString(Double source, String targetStr) {
        if (source == null && isEmpty(targetStr)) {
            return true;
        }
        final BigDecimal sourceDecimal = source == null ? null : BigDecimal.valueOf(source).setScale(3, RoundingMode.HALF_UP);
        return safeCompare(sourceDecimal, convert(targetStr));
    }

    public static String denull(String strNullable) {
        return strNullable == null ? "" : strNullable;
    }

    private static final String EN_PUNCTUATION_REGEXP = "[0-9a-zA-Z,.\\-()*+#!]";

    /**
     * 计算一个字符串在oracle中的长度：
     * 正常情况下：一个中文字符在oracle占2个字节
     *
     * @param str
     * @return
     * @since 2020-06-22
     */
    public static int calcOracleLength(String str) {
        String cnStr = str.replaceAll(EN_PUNCTUATION_REGEXP, "");
        return cnStr.length() + str.length();
    }

    /**
     * 转半角的函数(DBC case)
     * 全角空格为12288，半角空格为32
     * 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
     *
     * @param input 任意字符串
     * @return 半角字符串
     */
    public static String SBCToDBC(String input) {
        if (StrUtils.isEmpty(input)) {
            return input;
        }
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                //全角空格为12288，半角空格为32
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                //其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    public static void main(String[] args) {
        System.out.println(SBCToDBC("hsh"));
    }
}
